let base64Str = ""
Page({
    data: {
        isShowNext: false,
        age: null,
        showPop: !0,
        photoTipsShow: false, // 是否打开相机
        isFrontCamera: !0,
        TipsTime: 3,
        completedListLoaded: !1,
        uploadPath: '',
        completedList: [ {
            id: 1,
            name: "AI检测是否包含人脸"
        }, {
            id: 2,
            name: "AI检测照片光线"
        }, {
            id: 3,
            name: "AI检测脸部是否清晰"
        }, {
            id: 4,
            name: "AI检测五官是否有遮挡"
        }, {
            id: 5,
            name: "AI检测面部初步属性"
        } ],
        imageSrc: '',
        face_list:"",
        ratio:1
    },
    handleClick:function(){
        wx.chooseImage({
          count: 1,
          sizeType:["compressed","original"],
          sourceType:["album","camera"],
          success:(res)=>{
            // 把图片路径展示到页面上
              this.setData({
                imageSrc:res.tempFilePaths[0]
              })
              wx.getImageInfo({
                src: res.tempFilePaths[0],
                success:(res)=>{
                  console.log(300/res.width);
                  this.setData({
                    ratio:300/res.width
                  })
                }
              })
            // 获取base64 编码
            this.getBASE64String(res.tempFilePaths[0]);
            this.getAccessToken();
          }
        })
    },
    // 获取百度智能云的 access_token
    getAccessToken:function(){
      wx.request({
        method:"get",
        url: 'https://aip.baidubce.com/oauth/2.0/token',
        data:{
          'grant_type': 'client_credentials',
          'client_id': 'stRN7mbhxq1nFuoMbSqlxjZG',
          'client_secret': 'ivzLFnITb6Np3QQl2ptyqTlDsBdPWIjm'
        },
        success:res=>{
          this.getFaceInfo(res.data.access_token);
        },
        fail:err=>{
          console.log(err);
        }
      })
    },
  
    getFaceInfo(token){
      wx.request({
        url:"https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token="+token,
        method:"POST",
        header:{
          "Content-Type":"application/json"
        },
        data:{
          image:base64Str,
          image_type:"BASE64",
          face_field:"beauty,age,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing"
        },
        dataType:"json",
        success:(res)=>{
          console.log(res);
          if(res.data.error_code == 0){
            console.log("ojb=>",res.data.result.face_list[0]);
              this.setData({
                face_list:res.data.result.face_list[0]
              })
          }else{
            console.log("程序报错，错误码为 =>",data.error_code);
            this.setData({
              obj:null
            })
          }
        }
  
      })
    },
  
    // 获取BASE64 String
    getBASE64String:function(filePath){
      let file = wx.getFileSystemManager();
      file.readFile({
        filePath,
        encoding:"base64",
        success:(res)=>{
          // console.log(res.data);
          base64Str = res.data;
        }
      })
    },
    toRecommend() {
        wx.navigateTo({
          url: '/pages/view/person-data/person-data',
        })
    },
    back: function() {
        wx.navigateBack({
            delta: 0,
            fail: function() {
                wx.reLaunch({
                    url: "/pages/index/index"
                });
            }
        });
    },
    photoPopNext: function() {
        this.setData({
            popNext: !0
        });
    },
    photoPopConfirm: function() {
        this.setData({
            showPop: !1,
            photoStatus: !0
        }), this.photoTipsHide();
    },
    photoTipsHide: function() {
        this.setData({
            photoTipsShow: !0
        });
    },
    positionChange: function() {
        this.setData({
            isFrontCamera: !this.data.isFrontCamera
        });
    },
    uploadAlbum: function() {
        if (console.log(this.data.photoTipsShow), this.data.photoTipsShow) {
            var e = this;
            wx.chooseImage({
                count: 1,
                sizeType: [ "compressed" ],
                sourceType: [ "album" ],
                success: function(t) {
                    var a = t.tempFilePaths[0];
                    e.setData({
                        tempImgPaths: a
                    }), e.selectphoto(1, "face_library");
                }
            });
        }
    },
    uploadPhoto: function() {
        var e = this;
        wx.createCameraContext().takePhoto({
            quality: "normal",
            success: function(t) {
                var a = t.tempImagePath;
                e.setData({
                    tempImgPaths: a
                })
                e.selectphoto(2, "face_camera");
            }
        });
    },
    selectphoto: function(e, t) {
        this.setData({
            photoTipsShow: false,
            photoType: e,
            shower: true,
            // slug: t
        })
        this.setTipsTime();
        this.setData({
            imageSrc: this.data.tempImgPaths
        })
        wx.getImageInfo({
            src: this.data.tempImgPaths,
            success:(res) => {
                console.log(300/res.width);
                this.setData({
                    ratio:375/res.width
                })
            }
        })
        this.getBASE64String(this.data.tempImgPaths);
        this.getAccessToken();
    },
    setTipsTime: function() {
        var e = this;
        this.data.TipsTime > 0 && (this.data.tipstime = setTimeout(function() {
            e.setData({
                TipsTime: e.data.TipsTime - 1
            }), e.setTipsTime();
        }, 1e3));
    },
    redoUpload: function() {
        this.setData({
            TipsTime: 3,
            shower: false,
            photoTipsShow: true,
            showResult: false,
            uploadSuccess: false,
            tempImg: false,
            showScan: false,
            showFreshCompletedList: []
        });
    },
    uploadConfirm: function() {
        wx.showLoading({
            title: "照片上传中...",
            mask: !0
        })
        this.setData({
            tempImg: true,
            shower: false,
            isShowNext: true
        })
        wx.hideLoading()
        
    },
    radioChange: function(e) {
        var t, a = this;
        t = "Female" === this.data.faceInfo.attributes.gender.value ? "女" : "男", e.detail == t ? this.setData({
            radio: e.detail
        }) : s.default.confirm({
            title: "AI判断性别与您不符",
            message: "请再次确定是否修改性别"
        }).then(function() {
            a.setData({
                radio: e.detail
            });
        }).catch(function() {});
    },
    showForm: function(e) {
        var t = this, a = e.data.face_num, o = e.data.faces[0];
        if (0 != a && a) if (Math.abs(o.attributes.headpose.pitch_angle) > 20 || Math.abs(o.attributes.headpose.roll_angle) > 45 || Math.abs(o.attributes.headpose.yaw_angle) > 20) (0, 
        i.default)("未识别到人脸"), setTimeout(function() {
            t.redoUpload();
        }, 2e3); else if (a > 1) (0, i.default)("照片非单人正面照"), setTimeout(function() {
            t.redoUpload();
        }, 2e3); else if (o.attributes.eyestatus.left_eye_status.occlusion > 70) (0, i.default)("眼睛被遮挡"), 
        setTimeout(function() {
            t.redoUpload();
        }, 2e3); else if (o.attributes.eyestatus.left_eye_status.normal_glass_eye_close > 70 || o.attributes.eyestatus.left_eye_status.normal_glass_eye_open > 70 || o.attributes.eyestatus.left_eye_status.dark_glasses > 70) (0, 
        i.default)("请摘除眼镜后拍照上传"), setTimeout(function() {
            t.redoUpload();
        }, 2e3); else if (e.width < 200 || e.height < 200) (0, i.default)("照片尺寸过小"), setTimeout(function() {
            t.redoUpload();
        }, 2e3); else {
            var s = "";
            wx.setStorageSync("faceInfo", o), s = "Female" === o.attributes.gender.value ? "女" : "男", 
            this.setData({
                radio: s,
                showScan: !1,
                ageValue: o.attributes.age.value,
                uploadSuccess: !0,
                faceInfo: o,
                imageWidth: e.width,
                imageHeight: e.height
            });
        } else (0, i.default)("未识别到人脸"), setTimeout(function() {
            t.redoUpload();
        }, 2e3);
    },
    footprint: function(e) {
        return a(t.default.mark(function a() {
            var o, i, s, n;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return o = wx.getStorageSync("footprint_photoId"), i = parseInt(Date.now() / 1e3), 
                    s = {
                        photo_id: o,
                        slug: e,
                        time: i
                    }, t.next = 5, u.service.index.footprint(s);

                  case 5:
                    n = t.sent, n.error, n.data;

                  case 8:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    preFeatures: function() {
        return a(t.default.mark(function e() {
            var a;
            return t.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return e.next = 2, u.service.face.preFeature();

                  case 2:
                    a = e.sent, a.error, a.data;

                  case 5:
                  case "end":
                    return e.stop();
                }
            }, e);
        }))();
    },
    repeatScan: function() {
        this.animate("#scan", [ {
            translateY: 0
        }, {
            translateY: "100vh"
        } ], 1e3, function() {}.bind(this));
    },
    onAgeChange: function(e) {
        this.setData({
            age: e.detail.value
        });
    },
    onHeightChange: function(e) {
        var t = e.detail.value;
        this.setData({
            bodyHeight: t
        });
    },
    onWeightChange: function(e) {
        var t = e.detail.value;
        this.setData({
            bodyWeight: t
        });
    },
    startAnalysis: function() {
        var e = this.data, t = e.bodyWeight, a = e.bodyHeight, o = e.age;
        return null === t || null === a || null === o ? (wx.showModal({
            title: "提示",
            content: "请填完信息哦~"
        }), !1) : o < 14 ? ((0, i.default)("本程序暂时未向未成年开放"), !1) : o > 80 ? ((0, i.default)("请检查年龄是否填写正确"), 
        !1) : (a < 140 || a > 190) && (n += 1) < 2 ? ((0, i.default)("请检查身高填写是否正确"), !1) : (t / 2 < 40 || t / 2 > 120) && (r += 1) < 2 ? ((0, 
        i.default)("请检查体重数值是否正确"), !1) : void this.gotoCanvas();
    },
    gotoCanvas: function() {
        var e = this;
        return a(t.default.mark(function a() {
            var s, n, r, d, c, h, l, f, p, g, m, w, S, v, x, y, T, b, I, D;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return s = e.data, n = s.filePath, r = s.age, d = s.faceInfo, c = s.imageWidth, 
                    h = s.imageHeight, l = s.radio, f = s.bodyWeight, p = s.bodyHeight, g = s.tempImgPaths, 
                    wx.showLoading({
                        title: "加载中..."
                    }), wx.setStorageSync("filePath", n), r = r || d.attributes.age.value, m = l, w = f / 2, 
                    S = p, v = wx.getSystemInfoSync(), x = v.brand, y = v.model, T = x + " " + y, o.default.info("device", T), 
                    t.next = 14, u.service.face.neWfeature(m, r, S, w, T);

                  case 14:
                    b = t.sent, I = b.error, D = b.data, I ? (o.default.info("error", I), (0, i.default)("检测失败"), 
                    wx.switchTab({
                        url: "/pages/index/index"
                    })) : 0 === D.code ? (wx.setStorageSync("photoId", D.photoId), wx.setStorageSync("faceImage", D.photo), 
                    wx.setStorageSync("faceImageWidth", c), wx.setStorageSync("faceImageHeight", h), 
                    wx.setStorageSync("bodyHeight", S), wx.setStorageSync("bodyWeight", w), wx.setStorageSync("age", r), 
                    wx.setStorageSync("gender", m), wx.setStorageSync("faceFeatureInfo", D.data), wx.hideLoading(), 
                    wx.reLaunch({
                        url: "/packageD/pages/faceCanvas/faceCanvas?photo=" + g + "&imageWidth=" + c + "&imageHeight=" + h
                    })) : (wx.hideLoading(), (0, i.default)(D.msg));

                  case 18:
                  case "end":
                    return t.stop();
                }
            }, a);
        }))();
    },
    onLoad: function(e) {},
    onReady: function() {},
    onShow: function() {
        var e = wx.getSystemInfoSync(), t = wx.getStorageSync("token");
        this.setData({
            token: t,
            screenWidth: e.windowWidth,
            screenHeight: e.windowHeight,
            statusBarHeight: e.statusBarHeight
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});